
// 搭建服务器
const fs = require('fs')
require('http').createServer((req, res)=>{
    // 此时，所有请求都响应 1111
    // res.end('111')

    // 目的：不同的请求地址响应不同的内容
    // req.url - 请求行中的请求路径
    /*let url = req.url
    if(url === '/'){
        // 读取文件并响应
        let data = fs.readFileSync('./project/index.html')
        res.end(data)
    }else if(url === '/js/index.js'){
        let data = fs.readFileSync('./project/js/index.js')
        res.end(data)
    }else if(url === '/css/index.css'){
        let data = fs.readFileSync('./project/css/index.css')
        res.end(data)
    }else if(url === '/favicon.ico'){
        let data = fs.readFileSync('./project/favicon.ico')
        res.end(data)
    }
    // url === /font/iconfont.woff?t=1479642403788
    else if(url.includes('.woff') || url.includes('.ttf')){
        url = url.split('?')[0]
        console.log(url);
        let data = fs.readFileSync('./project' + url)
        res.end(data)
    }
    
    else{
        let data = fs.readFileSync('./project'+url)
        res.end(data)
    }*/

    let url = req.url;
    if(url.startsWith('/api')){
        if(url === '/api/register'){
            let str = '';
            req.on('data',chunk=>{
                str += chunk.toString()
            })
            req.on('end',()=>{
                let arr = str.split('&')
                let obj = {}
                arr.forEach(item=>{
                    let brr = item.split('=')
                    obj[brr[0]] = brr[1]
                })
                let data = fs.readFileSync("./project/src/json/data.json")
                data = data.toString()
                let pbj = {
                    code: 0,
                    msg: '用户名被占用了'
                }
                if(data === ''){
                console.log(1);
                    data = []
                    data.push(obj)
                    fs.writeFileSync('./project/src/json/data.json', JSON.stringify(data))
                    pbj = {
                        code: 1,
                        msg: '注册成功'
                    }
                    res.end(JSON.stringify(pbj))
                }
                data = JSON.parse(data)
                let index = data.findIndex(item=>item.tel === obj.tel)
                console.log(index,data,obj);
                if(index<0){
                    console.log(2);
                    data.push(obj)
                    data = JSON.stringify(data)
                    fs.writeFileSync('./project/src/json/data.json', data)
                    pbj = {
                        code: 1,
                        msg: '注册成功'
                    }
                }
                console.log(pbj);
                res.end(JSON.stringify(pbj))
            })
        }else if(url === '/api/login'){
            // 接收post数据
            let str = '';
            req.on('data',chunk=>{
                str += chunk.toString()
            })
            req.on('end',()=>{
                let arr = str.split('&')
                let obj = {};
                arr.forEach(item=>{
                    let brr = item.split('=')
                    obj[brr[0]] = brr[1]
                })
                // 读取data.json中的所有数据
                let data = fs.readFileSync('./project/src/json/data.json')
                data = JSON.parse(data)
                // 判断data中是否有obj的数据
                let index = data.findIndex(item=>item.tel === obj.tel && item.password === obj.password)
                if(index<0){
                    let pbj = {
                        code: 0,
                        msg: "用户名或密码错误"
                    }
                    res.end(JSON.stringify(pbj))
                }else{
                    let pbj = {
                        code: 1,
                        msg: "登录成功"
                    }
                    res.end(JSON.stringify(pbj))
                }
            })
        }
    }else if(req.url === '/layer/theme/default/layer.css?v=3.5.1'){
        let data = fs.readFileSync('./project/src/layer/theme/default/layer.css')
        res.end(data)
    }else{
        if(url === '/'){
            url = '/index.html'
        }else if(url.includes('.woff') || url.includes('.ttf')){
            url = url.split('?')[0]
        }
        //如果路径中包含？，就将后面的数据去掉，保留文件 名称部分，方便后面读取文件
        else if(url.includes('?')){
            let index = url.indexOf('?')
            url = url.slice(0,index)
        }
        let data = fs.readFileSync('./project/src' + url)
        res.end(data)
    }
}).listen(3000)